package com.winchain.chainbill.acct.controller;

import com.shell.bee.acct.account.po.AcctCard;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.po.AcctTxWdrl;
import com.shell.bee.acct.transaction.vo.AcctInfo;
import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.auth.AuthOrgan;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.CommonFileUtil;
import com.winchain.chainbill.acct.service.MyAcctService;
import com.winchain.chainbill.acct.vo.AcctVo;
import com.winchain.chainbill.acct.vo.ChainTxVo;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.util.ExcelGenUtil;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyAcctController {
	@Autowired
	private MyAcctService myAcctService;
	@Autowired
	private BillAcctService billAcctService;

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public AcctVo getMyAcct() {
		return this.myAcctService.getSettleAcct();
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������������������")
	public List<AcctVo> getGrpAcctBlc() {
		String cusId = CommonDataUtil.getCurEntId();
		return this.myAcctService.getGrpAcctBlc(cusId);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public List<AcctTxRec> getAcctTx(PageLimit pageLimit, String acctNo, String txType, String txDateStart,
			String txDateEnd) {
		return this.myAcctService.getAcctTx(pageLimit, acctNo, txType, txDateStart, txDateEnd);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public Map<String, BigDecimal> getAcctTxStat(String acctNo, String txType, String txDateStart, String txDateEnd) {
		return this.myAcctService.getAcctTxStat(acctNo, txType, txDateStart, txDateEnd);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public List<AcctTxWdrl> getWithdrawList(PageLimit pageLimit, String cusName, String wdrlState,
			String applyDateStart, String applyDateEnd, String approvers) {
		String[] approverArr = null;
		if (StringUtil.notEmpty(approvers)) {
			approverArr = approvers.split(",");
		}
		return this.myAcctService.getWithdrawList(pageLimit, cusName, wdrlState, applyDateStart, applyDateEnd,
				approverArr);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������")
	public List<AcctCard> getWithdrawAccts() {
		String cusId = CommonDataUtil.getCurEntId();
		return this.billAcctService.getCards(cusId, "0");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public void withdrawListExport(PageLimit pageLimit, String cusName, String wdrlState, String applyDateStart,
			String applyDateEnd, String approvers, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] approverArr = null;
		if (StringUtil.notEmpty(approvers)) {
			approverArr = approvers.split(",");
		}
		List<AcctTxWdrl> wdrls = this.myAcctService.getWithdrawList(pageLimit, cusName, wdrlState, applyDateStart,
				applyDateEnd, approverArr);
		Map<String, Object> map = new HashMap();
		map.put("datas", wdrls);
		InputStream is = ExcelGenUtil.getInstance().GenerateExcelForStream("/template/excel/withdrawListExport.xlsx",
				map);
		CommonFileUtil.writeFileToClient("������������.xlsx", is, response, request);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public RetMsg withdraw(AcctTxRec txRec, String grpId) {
		AcctInfo acctInfo = new AcctInfo();
		acctInfo.setCustId(CommonDataUtil.getCurEntId());
		acctInfo.setCurrCode(AssetCode.CNY.name());
		acctInfo.setAcctType("1");
		acctInfo.setUseCode("00");
		acctInfo.setBkpPrdCode(grpId);
		acctInfo = this.billAcctService.getAcctCardInfo(acctInfo);
		if (txRec.getTxAmt().compareTo(acctInfo.getAvailableAmt()) >= 1) {
			return new RetMsg("warning", false, "��������������������������������������������������!");
		}
		this.myAcctService.withdraw(txRec, grpId);
		AuthOrgan organ = CommonDataUtil.getCurEnt();
		if ("FINNER".equals(organ.getCusType())) {
			return new RetMsg("success", true, "��������!");
		}
		return new RetMsg("success", true, "����������������������������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����")
	public RetMsg recharge(AcctTxRec txRec, String grpId) {
		this.myAcctService.recharge(txRec, grpId);
		return new RetMsg("success", true, "����������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public RetMsg withdrawPass(String wdrlIds) {
		return this.myAcctService.withdrawPass(wdrlIds);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public RetMsg withdrawReject(String wdrlIds, String remark) {
		return this.myAcctService.withdrawReject(wdrlIds, remark);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������")
	public Map<String, Map<String, String>> getChainAssets() {
		return this.myAcctService.getChainAssets();
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������������")
	public List<ChainTxVo> getChainTx(PageLimit pageLimit, String acctNo, String billCode, String txType) {
		return this.myAcctService.getChainTx(pageLimit, acctNo, billCode, txType);
	}
}
